home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14391 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  103 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: #include "" for large
  5. Message-ID: <DpuM5o.8LI@rci.ripco.com>
  6. X-Nntp-Sender: mambuhl@foley.ripco.com
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Sun, 14 Apr 1996 10:51:22 GMT
  10.  
  11. "Carlos Diaz (CS)" <cdiaz@eng.usf.edu>
  12. in <Pine.SUN.3.92.960411195730.24973A-100000@suntan> asks:
  13.  
  14. [I have included the entire question from cdiaz at EOM, so that anyone
  15.  who might be a better parser than I but missed the original posting
  16.  might be able to help him.]
  17.  
  18. Since you don't actually tell us what you are doing, I will guess that
  19. you have something like:
  20.  
  21. /* --- situation 1 --- */
  22. /* part2.h */
  23.         #define EQUAL 0
  24.         int somefunction(int);
  25. /* main.c */
  26.         #include "part2.h"
  27.         int main() {
  28.                 /* code */
  29.                 return 0;
  30.         }
  31. /* part2.c */
  32.         int somefunction(int somearg)
  33.         {
  34.                 return (somearg == EQUAL);
  35.         }
  36.  
  37. This properly would give a diagnostic that EQUAL is undefined in
  38. part2.c.
  39.  
  40. /* --- situation 2 --- */
  41. /* part2.h */
  42.         #define EQUAL 0
  43.         int somefunction(int);
  44. /* main.c */
  45.         #include "part2.h"
  46.         #define EQUAL 0
  47.         int main() {
  48.                 /* code */
  49.                 return 0;
  50.         }
  51. /* part2.c */
  52.         int somefunction(int somearg)
  53.         {
  54.                 return (somearg == EQUAL);
  55.         }
  56.  
  57. This properly would give a diagnostic that EQUAL is undefined in
  58. part2.c and a diagnostic that EQUAL is multiply defined in main.c.
  59.  
  60. /* -- An answer, if the above is what is happening -- */
  61. Put your #defines into a header (as you are doing):
  62. /* defines.h */
  63.         #define EQUAL 0
  64.         int somefunction(int);
  65. Do _not_ put #defines in the *.c files, but in each, include the
  66. definition header:
  67. /* part1.c */
  68.         #include "defines.h"
  69.         int main() {
  70.                 /* code */
  71.                 return 0;
  72.         }
  73. /* part2.c */
  74.         #include "defines.h"
  75.         int somefunction(int somearg)
  76.         {
  77.                 return (somearg == EQUAL);
  78.         }
  79.  
  80.  
  81. [Original question follows]
  82.  
  83. > I've been writing short two part programs using the #include "filename"
  84. >preprocessor.
  85. > I call one file main.c and the other part2.c, for which there is a
  86. >prototype header file part2.h.
  87. > All works well when I compile the programs, except when  part2.h has
  88. >#define constants (I've been advised by my professor to use #define for
  89. >constants over 'const type var_name'). My compiler returns a fatal error
  90. >reporting that the symbol in the #define is not defined.
  91. > For example if #define EQUAL 0 is part of part2.h, I'm told that the
  92. >symbol EQUAL is not defined. But if I put the #define inside main.c, I'm
  93. >told that I've defined EQUAL  TWICE, once in part2.h and again within
  94. >main. The book we're using in class is not helping at all in this subject,
  95. >and I cannot figure out why everything else is recognized, except #define
  96. >constants. Can anyone here help? If the answer to this is in the FAQ, just
  97. >tell me where to download it from. Thanks!
  98.  
  99.                             
  100. --
  101. * Martin Ambuhl       net: mambuhl@ripco.com
  102. * Chicago, IL (USA)    
  103.